package com.rem.chat.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rem.chat.pojo.dataobject.UserContactApplyDO;
import com.rem.chat.service.dto.ContactApplyDTO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * @author: Themberfue
 * @date: 2025/6/7 10:51
 * @description:
 */
@Mapper
public interface UserContactApplyMapper extends BaseMapper<UserContactApplyDO> {
    /**
     * 分页查询用户收到的和发出的所有好友/群组申请
     */
    @Select("(" +
            "SELECT a.*, " +
            "CASE " +
            "  WHEN a.contact_type = 0 THEN u_apply.nick_name " +
            "  WHEN a.contact_type = 1 THEN g.group_name " +
            "END AS contact_name " +
            "FROM user_contact_apply a " +
            "LEFT JOIN user_info u_apply ON u_apply.user_id = a.apply_user_id " +
            "LEFT JOIN group_info g ON g.group_id = a.contact_id " +
            "WHERE a.receive_user_id = #{userId}" +
            ") " +
            "UNION ALL " +
            "(" +
            "SELECT a.*, " +
            "CASE " +
            "  WHEN a.contact_type = 0 THEN u_receive.nick_name " +
            "  WHEN a.contact_type = 1 THEN g.group_name " +
            "END AS contact_name " +
            "FROM user_contact_apply a " +
            "LEFT JOIN user_info u_receive ON u_receive.user_id = a.receive_user_id " +
            "LEFT JOIN group_info g ON g.group_id = a.contact_id " +
            "WHERE a.apply_user_id = #{userId}" +
            ") " +
            "ORDER BY last_apply_time DESC")
    Page<ContactApplyDTO> selectUserContactApplies(Page<UserContactApplyDO> page, @Param("userId") String userId);
}
